package com.example.helper2;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.content.Intent;
import android.net.Uri;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import androidx.recyclerview.widget.SimpleItemAnimator;

import android.widget.Toast;

import com.example.helper2.adapter.LeftAdapter;
import com.example.helper2.adapter.RightAdapter;
import com.example.helper2.base.SimpleRecyclerAdapter;
import com.example.helper2.bean.SortBean;
import com.example.helper2.bean.SortItem;
import com.example.helper2.contants.ItemType;
import com.example.helper2.utils.MyUtils;

public class fragment_defend_ways extends Fragment {
    private RecyclerView leftRecyclerView;
    private RecyclerView rightRecyclerView;

    private LeftAdapter leftAdapter;
    private RightAdapter rightAdapter;

    private final List<SortBean> mLeftList = new ArrayList<>();

    private final List<SortItem> mRightList = new ArrayList<>();

    private final Map<Integer, Integer> indexMap = new HashMap<>();

    private final List<String> titleList= new ArrayList<>();
    private final List<String> urlList=new ArrayList<>();

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
        View view=inflater.inflate(R.layout.defend_ways,container,false);
        leftRecyclerView = (RecyclerView) view.findViewById(R.id.rv_sort_left) ;
        rightRecyclerView = (RecyclerView) view.findViewById(R.id.rv_sort_right);
        // 左列表
        leftRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        ((SimpleItemAnimator) leftRecyclerView.getItemAnimator()).setSupportsChangeAnimations(false);
        leftAdapter = new LeftAdapter();
        leftAdapter.setListData(mLeftList);
        leftRecyclerView.setAdapter(leftAdapter);

        // 左侧列表的点击事件
        leftAdapter.setOnItemClickListener(new SimpleRecyclerAdapter.OnItemClickListener<SortBean>() {
            @Override
            public void onItemClick(SortBean item, int index) {
                // 左侧选中并滑到中间位置
                leftAdapter.setSelectedPosition(index);
                MyUtils.moveToMiddle(leftRecyclerView, index);
                // 右侧滑到对应位置
                ((LinearLayoutManager)rightRecyclerView.getLayoutManager())
                        .scrollToPositionWithOffset(indexMap.get(index),0);
            }
        });
        // 右列表
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
        rightRecyclerView.setLayoutManager(linearLayoutManager);
        rightAdapter = new RightAdapter();
        rightAdapter.setListData(mRightList);
        rightRecyclerView.setAdapter(rightAdapter);
        //监听rightAdapter的点击事件
        rightAdapter.setOnItemClickListener(new SimpleRecyclerAdapter.OnItemClickListener<SortItem>() {

            public void onItemClick(SortItem item, int index) {
                Toast.makeText(getActivity(), item.name, Toast.LENGTH_SHORT).show();
                Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(item.webUrl));
                startActivity(i);
            }
        });

        //右侧列表的滚动事件
        rightRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                //获取右侧列表的第一个可见Item的position
                int topPosition = ((LinearLayoutManager) rightRecyclerView.getLayoutManager()).findFirstVisibleItemPosition();
                // 如果此项对应的是左边的大类的index
                if (mRightList.get(topPosition).position != -1) {
                    MyUtils.moveToMiddle(leftRecyclerView, mRightList.get(topPosition).position);
                    leftAdapter.setSelectedPosition(mRightList.get(topPosition).position);
                }

            }
        });
        return view;
    }

    {
        titleList.add("电动口罩有没有用？");
        titleList.add("特殊时期，正确开会应该这样做");
        titleList.add("出门购物别大意，4招教你安心买");
        titleList.add("疫情期间，坐电梯需要注意什么？");
        titleList.add("如何辨别口罩真假");
        urlList.add("https://mp.weixin.qq.com/s/c98MyhQiJ3aiNWjjseYGbg");
        urlList.add("https://m.v.qq.com/play.html?cid=mzc0020028aofft&vid=a3066tzuwtl&ptag=v_qq_com%23v.play.adaptor%233");
        urlList.add("https://v.qq.com/x/page/e30692xbyrp.html");
        urlList.add("https://v.qq.com/x/cover/mzc0020028aofft/v3066hhvrz1.html");
        urlList.add("https://mp.weixin.qq.com/s/nAq1Au3wrXGD8g35Aj_01w");
        titleList.add("特殊时期，孩子感冒咳嗽了要不要去医院？");
        titleList.add("因为疫情耽误的疫苗接种，该如何安排补种？");
        titleList.add("孩子误服消毒清洁产品应该怎么办？");
        titleList.add("如果新型冠状病毒长期存在，儿童要如何防护？");
        titleList.add("宝宝体温37度多一点算发烧吗？");
        urlList.add("https://mp.weixin.qq.com/s/T9QvVHgkE30lMn_fC3EBBw");
        urlList.add("https://mp.weixin.qq.com/s/dejV_MZSZSZvlpSliER1dQ");
        urlList.add("https://mp.weixin.qq.com/s/mbLr9Ru7l8fvdzPfTTIe1Q");
        urlList.add("https://mp.weixin.qq.com/s/3shWjDpSrVKLJ3dJ2W8uCQ");
        urlList.add("https://mp.weixin.qq.com/s/LwHwWo9OCD_8EBmLS_lSPA");
        titleList.add("长时间戴口罩脸被压迫，做好这7点可减少皮肤损伤");
        titleList.add("医务人员确诊1716例，高危人群该如何防护？");
        titleList.add("守得住疫情防线，心理防线又该如何去防守？");
        titleList.add("医务人员下班后，会把新冠病毒带出医院吗？");
        titleList.add("新冠肺炎指南全解读（内含资料包）");
        urlList.add("https://mp.weixin.qq.com/s/derb_ScHvcdsvKEylToDzg");
        urlList.add("https://mp.weixin.qq.com/s/nHAC-MlPkSnqYwby7qOo8g");
        urlList.add("https://mp.weixin.qq.com/s/UfFxd6ZkHx6ZQIPnCcSDAQ");
        urlList.add("https://mp.weixin.qq.com/s/u_nriShslwmKytJLSVZ3sg");
        urlList.add("https://mp.weixin.qq.com/s/3wi3HVZgQ9_aRMeGjslRPA");
        titleList.add("有些食物不能放冰箱！正确存放省钱又省心");
        titleList.add("体温多高算发烧？");
        titleList.add("洗手最重要的步骤！");
        titleList.add("别抢维生素C了！");
        titleList.add("在办公室怕被传染？一个习惯要警惕");
        urlList.add("https://v.qq.com/x/page/e3071mcmten.html");
        urlList.add("https://mp.weixin.qq.com/s/K18HAy6puXd218iY1X4xGQ");
        urlList.add("https://mp.weixin.qq.com/s/49xBpne9O9tmkTCL8BBEvQ");
        urlList.add("https://mp.weixin.qq.com/s/H5flsElx9YP0BCtTRrCkZQ");
        urlList.add("https://v.qq.com/x/page/y3066zsggry.html");
        titleList.add("家有老人，如何让家里环境更安全？");
        titleList.add("老人坚持外出，应该做好哪些防护？");
        titleList.add("上了年纪，这类口罩并不建议戴");
        titleList.add("老人要看病，如何保证就医安全？");
        titleList.add("老人更同意感染新型冠状病毒吗？");
        urlList.add("https://wechat.dxy.cn/japi/weixin/news/42873/WhrhlBJCLLTXq");
        urlList.add("https://wechat.dxy.cn/japi/weixin/news/42876/X7sA8rd92mBYv");
        urlList.add("https://wechat.dxy.cn/news/view?noshare=false&watermark=false&nocopy=false&email=false&simuri=%2Fjapi%2Fweixin%2Fnews%2F42875%2FGlK7tbsGKKohE%2Fdata&teamId=8");
        urlList.add("https://wechat.dxy.cn/news/view?noshare=false&watermark=false&nocopy=false&email=false&simuri=%2Fjapi%2Fweixin%2Fnews%2F42891%2Fkc51jTxgWeNPc%2Fdata&teamId=8");
        urlList.add("https://wechat.dxy.cn/japi/weixin/news/42870/TBwolARflSz6J");
        titleList.add("新冠病毒会不会由孕产妈妈传给宝宝？");
        titleList.add("新冠肺炎真的存在母婴垂直传播吗？");
        titleList.add("疫情当前，还能去做产检吗？");
        titleList.add("孕期如何预防新冠肺炎？");
        titleList.add("孕期如何加强防护？");
        urlList.add("https://mp.weixin.qq.com/s/EzlY3PGWND5mZfBg8qQeqw");
        urlList.add("https://mp.weixin.qq.com/s/ftDcCBvZgetuId44knxX8A");
        urlList.add("https://wechat.dxy.cn/news/view?noshare=false&watermark=false&nocopy=false&email=false&simuri=%2Fjapi%2Fweixin%2Fnews%2F42913%2FJzGa6N6FFxAq8%2Fdata&teamId=101");
        urlList.add("https://wechat.dxy.cn/news/view?noshare=false&watermark=false&nocopy=false&email=false&simuri=%2Fjapi%2Fweixin%2Fnews%2F42912%2FbXH5aBTMz0ylf%2Fdata&teamId=101");
        urlList.add("https://wechat.dxy.cn/news/view?noshare=false&watermark=false&nocopy=false&email=false&simuri=%2Fjapi%2Fweixin%2Fnews%2F42807%2FqWKEVuaue28ro%2Fdata&teamId=101");







        // 构造点数据，比如整个数据刚刚好就是从json转过来的，一个Bean里面有一个大类，有若干个小类
        // 左侧的adapter就直接用这个构造好的list
        SortBean bean1 = new SortBean();
        bean1.bigSortId = 1;
        bean1.bigSortName = "我要出行";
        List<SortBean.ListBean> list1 = new ArrayList<>();
        for (int j = 0; j < 5; j++) {
            SortBean.ListBean listBean = new SortBean.ListBean();
            listBean.smallSortId = j;
            listBean.smallSortName = titleList.get(j);
            listBean.webUrl=urlList.get(j);
            list1.add(listBean);
        }
        bean1.list = list1;
        mLeftList.add(bean1);

        SortBean bean2 = new SortBean();
        bean2.bigSortId = 2;
        bean2.bigSortName = "家有小孩";
        List<SortBean.ListBean> list2 = new ArrayList<>();
        for (int j = 0; j < 5; j++) {
            SortBean.ListBean listBean = new SortBean.ListBean();
            listBean.smallSortId = j;
            listBean.smallSortName = titleList.get(j+5);
            listBean.webUrl=urlList.get(j+5);
            list2.add(listBean);
        }
        bean2.list = list2;
        mLeftList.add(bean2);

        SortBean bean3 = new SortBean();
        bean3.bigSortId = 3;
        bean3.bigSortName = "给医务者";
        List<SortBean.ListBean> list3 = new ArrayList<>();
        for (int j = 0; j < 5; j++) {
            SortBean.ListBean listBean = new SortBean.ListBean();
            listBean.smallSortId = j;
            listBean.smallSortName = titleList.get(j+10);
            listBean.webUrl=urlList.get(j+10);
            list3.add(listBean);
        }
        bean3.list = list3;
        mLeftList.add(bean3);

        SortBean bean4 = new SortBean();
        bean4.bigSortId = 4;
        bean4.bigSortName = "我宅在家";
        List<SortBean.ListBean> list4 = new ArrayList<>();
        for (int j = 0; j < 5; j++) {
            SortBean.ListBean listBean = new SortBean.ListBean();
            listBean.smallSortId = j;
            listBean.smallSortName = titleList.get(j+15);
            listBean.webUrl=urlList.get(j+15);
            list4.add(listBean);
        }
        bean4.list = list4;
        mLeftList.add(bean4);

        SortBean bean5 = new SortBean();
        bean5.bigSortId = 5;
        bean5.bigSortName = "家有老人";
        List<SortBean.ListBean> list5 = new ArrayList<>();
        for (int j = 0; j < 5; j++) {
            SortBean.ListBean listBean = new SortBean.ListBean();
            listBean.smallSortId = j;
            listBean.smallSortName = titleList.get(j+20);
            listBean.webUrl=urlList.get(j+20);
            list5.add(listBean);
        }
        bean5.list = list5;
        mLeftList.add(bean5);

        SortBean bean = new SortBean();
        bean.bigSortId = 1;
        bean.bigSortName = "家有孕妇";
        List<SortBean.ListBean> list = new ArrayList<>();
        for (int j = 0; j < 5; j++) {
            SortBean.ListBean listBean = new SortBean.ListBean();
            listBean.smallSortId = j;
            listBean.smallSortName = titleList.get(j+25);
            listBean.webUrl=urlList.get(j+25);
            list.add(listBean);
        }
        bean.list = list;
        mLeftList.add(bean);


        // 右侧的list是将每一个大类和小类按次序添加，并且标记大类的位置
        for (int i = 0; i < mLeftList.size(); i++) {
            SortItem bigItem = new SortItem();
            bigItem.viewType = ItemType.BIG_SORT;
            bigItem.id = mLeftList.get(i).bigSortId;
            bigItem.name = mLeftList.get(i).bigSortName;
            // 标记大类的位置，所有项的position默认是-1，如果是大类就添加position，让他和左侧位置对应
            bigItem.position = i;
            mRightList.add(bigItem);
            for (int j = 0; j < mLeftList.get(i).list.size(); j++) {
                SortItem smallItem = new SortItem();
                smallItem.viewType = ItemType.SMALL_SORT;
                smallItem.id = mLeftList.get(i).list.get(j).smallSortId;
                smallItem.name = mLeftList.get(i).list.get(j).smallSortName;
                smallItem.webUrl=mLeftList.get(i).list.get(j).webUrl;
                mRightList.add(smallItem);
            }
        }
        // 点击左侧需要知道对应右侧的位置，用map先保存起来
        for (int i = 0; i < mRightList.size(); i++) {
            if (mRightList.get(i).position != -1) {
                indexMap.put(mRightList.get(i).position, i);
            }
        }

    }

}






